/*
 * Created on 10.06.2004
 *
 */

package de.localsport.util;

/**
 * Calculates the player's score for his tip
 * according to the real result. 
 * 
 * @author MaS
 *
 */

import java.lang.Math;

public class ScoreCalculator {

	public static final int MATCHES_TYPE_GROUPMATCH = 0;
	public static final int MATCHES_TYPE_FINALMATCH = 1;
	public static final int MATCHES_TYPE_BET = 2;

	private int bet_id = -1;
	private int match_id = -1;
	private int home_guess = -1;
	private int guest_guess = -1;
	private int score = 0;
	private int home_score = -1;
	private int guest_score = -1;
	private int match_type = -1;
  
	public int getBet_id () {
		return bet_id ;
	}  
	public int getMatch_id () {
		return match_id ;
	}  
	public int getHome_guess () {
		return home_guess;
	}  
	public int getGuest_guess () {
		return guest_guess;
	}  
	public int getHome_score () {
		return home_score;
	}
	public int getGuest_score () {
		return home_guess;
	}  
	public int getScore () {
		return score;
	}
	public int getMatch_type () {
		return match_type;
	}
 
 	public void setBet_id (int i){
		bet_id = i;
	}  
	public void setMatch_id (int i){
		match_id = i;
	}  
	public void setHome_guess (int i){
		home_guess = i;
	}  
	public void setGuest_guess (int i){
		guest_guess = i;
	}  
	public void setHome_score (int i){
		home_score = i;
	}
	public void setGuest_score (int i){
		guest_score = i;
	}  
	public void setScore (int i){
		score = calcScore();
	}
	public void setMatch_type (int i){
		match_type = i;
	}
  
	public int calcScore() {
		int lscore = 0;
		switch (getMatch_type()) {
			case MATCHES_TYPE_GROUPMATCH:
			case MATCHES_TYPE_FINALMATCH:
				lscore = calcMatchScore();
				break;
			case MATCHES_TYPE_BET:
				lscore = calcBetScore();
				break;
			default:
				lscore = 0;
				break;
		}
		score = lscore;
		return lscore;
	}
  
  	private int calcBetScore() {
		int lscore = 0;
		// Check completeness
		if (home_score < 0 || home_guess < 0) {
			lscore = 0;
		// Check perfect guess
		} else {
			if (home_score == home_guess) lscore = 20;
			else lscore = 0;
		}
		return lscore;  	
	}
	
 	private int calcMatchScore() {
		int lscore = 0;
		// Check completeness
		if (home_score < 0 || guest_score < 0 || home_guess < 0 || guest_guess < 0) {
			lscore += 0;
		// Check perfect guess
		} else if (home_score == home_guess && guest_score == guest_guess) {
			lscore += 30;
		// Check tendency
		} else if (((home_score - guest_score) * (home_guess - guest_guess) > 0) ||
					 (home_score - guest_score == 0 && home_guess - guest_guess == 0)) {
			lscore += 10;
			// Check match result difference
			int rdiff = Math.abs ((home_score - guest_score) - (home_guess - guest_guess));
			switch (rdiff) {
				case 0:	lscore += 8; break;
				case 1:	lscore += 4; break;
				case 2:	lscore += 2; break;
				case 3: lscore += 1; break;
				default: lscore += 0; break;
			}
			// Check goal difference
			int gdiff = Math.abs (home_score - home_guess) + Math.abs (guest_score - guest_guess);
			switch (gdiff) {
				case 1:	lscore += 4; break;
				case 2:	lscore += 2; break;
				case 3: lscore += 1; break;
				default: lscore += 0; break;
			}
		}
		return lscore;  	
	}

	public static void main(String[] args) {
	}
}
